package com.sry.blog.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.sry.blog.common.api.CommonResult;
import com.sry.blog.sys.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @Value("${file.url}")
    private String FILE_URL;

    @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
    public CommonResult uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return CommonResult.failed("文件为空，请重新上传！");
        }
        String filename = fileService.upload(file);
        if (StrUtil.isNotEmpty(filename)) {
            return CommonResult.success(filename);
        } else {
            return CommonResult.failed("上传失败");
        }
    }
    @RequestMapping(value = "/uploadMarkDown",method = RequestMethod.POST)
    public CommonResult uploadMarkDown(@RequestBody String markDown,@RequestParam("path") String path) throws IOException {
        if (StrUtil.isNotEmpty(markDown)) {
            return CommonResult.success(fileService.uploadMarkDown(markDown,path));

        } else {
            return CommonResult.failed("上传失败");
        }
    }

    @RequestMapping(value = "/getMarkDown",method = RequestMethod.GET)
    public CommonResult getMarkDown(@RequestParam("id") Integer id) throws IOException {
        if (id != null) {
            return CommonResult.success(fileService.getMarkDown(id));
        } else {
            return CommonResult.failed("获取失败");
        }
    }
}
